package com.no1ikun.view;

import com.no1ikun.service.StudentService;
import com.no1ikun.service.StudentServiceImpl;
import com.no1ikun.util.CalendarPanel;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJBox;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InsertJPanel extends JPanel {
    private MyJTextField nameEdit;
    private MyJTextField passwordEdit;
    private MyJBox sexEdit;
    private MyJTextField birthdayEdit;
    private MyJTextField earth_Edit;
    private MyJBox dept_Edit;
    private MyJBox cno_Edit;
    private StudentService ss = new StudentServiceImpl();

    public InsertJPanel() {
        //初始化数据
        init();
    }


    public void init() {
        this.setLayout(null);//绝对布局
        this.setBounds(80, 80, 885, 452);
        this.setBackground(Color.WHITE);
        //添加文字
        MyJLabel maxText = new MyJLabel("学生注册信息", 200, 40, 140, 10, "微软雅黑", 1, 24, new Color(28, 136, 201));
        MyJLabel nameText = new MyJLabel("姓名", 40, 25, 110, 60, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel passwordText = new MyJLabel("密码", 40, 25, 110, 110, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel sexText = new MyJLabel("性别", 40, 25, 110, 160, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel birthdayText = new MyJLabel("生日", 40, 25, 110, 210, "微软雅黑", 1, 17, new Color(28, 136, 201));

        MyJLabel earth_place = new MyJLabel("生源地", 60, 25, 90, 260, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel dept = new MyJLabel("院系", 40, 25, 110, 310, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel cno = new MyJLabel("班级", 40, 25, 110, 360, "微软雅黑", 1, 17, new Color(28, 136, 201));


        //添加编辑框
        nameEdit = new MyJTextField(160, 60, 235, 25);
        nameEdit.setBorder(new LineBorder(new Color(28, 136, 201)));
        passwordEdit = new MyJTextField(160, 110, 235, 25);
        passwordEdit.setBorder(new LineBorder(new Color(28, 136, 201)));
        sexEdit = new MyJBox(new String[]{"男","女"},160, 160, 235, 25,Color.white,Color.black);
        birthdayEdit = new MyJTextField(160, 210, 235, 25);
        birthdayEdit.setBorder(new LineBorder(new Color(28, 136, 201)));
        birthdayEdit.setText("2000-01-01");
        CalendarPanel calendarPanel = new CalendarPanel(birthdayEdit, "yyyy-MM-dd");
        calendarPanel.initCalendarPanel();
        calendarPanel.setBounds(150,40,251, 245);
        JLabel tips = new JLabel("日历面板");
        calendarPanel.add(tips);
        earth_Edit=new MyJTextField(160, 260, 235, 25);
        earth_Edit.setBorder(new LineBorder(new Color(28, 136, 201)));
        dept_Edit=new MyJBox(new String[]{"计算机科学与技术学院","通信与信息工程学院"},160, 310, 235, 25,Color.white,Color.black);
        cno_Edit= new MyJBox(new String[]{"一班","二班","三班","四班"},160, 360, 235, 25,Color.white,Color.black);
        //添加两个按钮
        MyButton insert = new MyButton("添加", 180, 400, 70, 25, new Color(28, 136, 201), Color.WHITE);
        MyButton reset = new MyButton("重置", 300, 400, 70, 25, new Color(28, 136, 201), Color.WHITE);

        this.add(calendarPanel);
        this.add(insert);
        this.add(reset);
        this.add(nameEdit);
        this.add(passwordEdit);
        this.add(sexEdit);
        this.add(birthdayEdit);
        this.add(earth_Edit);
        this.add(cno_Edit);
        this.add(birthdayEdit);
        this.add(dept_Edit);

        this.add(maxText);
        this.add(nameText);
        this.add(passwordText);
        this.add(sexText);
        this.add(birthdayText);
        this.add(earth_place);
        this.add(dept);
        this.add(cno);


        //重置功能完成监听事件
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //将所有控件的内容重置就可以了
                nameEdit.setText("");
                passwordEdit.setText("");
//                sexEdit.setText("");
                earth_Edit.setText("");
                birthdayEdit.setText("2000-01-01");
            }
        });

        //注册功能
        //思路:1.获取前端提交过来的数据,去数据库添加数据,返回结果判断,给前端完成显示操作

        insert.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //注册按钮的点击事件
                //获取前端的数据
                String name = nameEdit.getText();
                String password = passwordEdit.getText();
                String sex = sexEdit.getText();

                String birthday = birthdayEdit.getText();
                String earth_place = earth_Edit.getText();
                String dept_Text=dept_Edit.getText();
                System.out.println(dept_Text);
                int Cno=cno_Edit.getIndex()+1;

                System.out.println("姓名："+name+",密码:"+password+",性别:"+sex+",生日:"+birthday+",生源地:"+earth_place+"院系:"+dept_Text+"班级:"+Cno);

                //判断用户输入的数据是否是空的
                if (!name.equals("") && !password.equals("") && !sex.equals("") && !earth_place.equals("") && !birthday.equals("")) {
                    //要去数据库添加数据
                    int i = ss.insertStudent(name,password,sex,birthday,earth_place,Cno);
//                    int i=1;
                    if (i > 0) {
                        JOptionPane.showMessageDialog(null, "恭喜添加成功!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                        //关闭自己,跳转到登录界面
                        nameEdit.setText("");
                        passwordEdit.setText("");
//                sexEdit.setText("");
                        earth_Edit.setText("");
                        birthdayEdit.setText("2000-01-01");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "注册信息不能为空!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                }


            }
        });


    }
}
